import re
import string

import model
from webtool import *

class ConcreteAdapter(model.Adapter):
    
    postListUrlTheme = r'http://www.newsmth.net/bbsdoc.php?board=%s&ftype=6'
    postListUrl = r'http://www.newsmth.net/bbsdoc.php?board=%s'
    postUrl=r'http://www.newsmth.net/bbstcon.php?board=%s&gid=%s'
    postContentUrl = r'http://www.newsmth.net/bbscon.php?bid=%s&id=%s'
    boardname = 'SecondMarket'
        
    def __init__(self):
        model.Adapter.__init__(self)
        
    
    def setBoardName(self,name):
        self.boardname = name
    
    def getName(self):
        return u'SMTH'
    
    def getPostList(self, thememode):
        list = []
        content = getPageText(self.getPageUrl(thememode))
        if content:
            ls = re.findall(r'(c\.o\(.*\)\;)', content, re.M)
            for l in ls:
                p = self.parsePost(l)
                if p:
                    list.append(p)
            self.setdata(content, list)
        return list
    
    def getPostDetail(self, post):
        if not post or not post.getData('bid'):
            return
        content = getPageText(self.postContentUrl %(post.getData('bid'), post.getID()))
        if content:
            ls = re.findall(r"\\n\\n.+\\n--", content,re.M)
            if len(ls):
                ctt = ls[0][4:-4]
                ctt = ctt.replace(u'\\n', u'\n')
                ctt = re.sub(r'\\r\[(\d*;\d*)*[a-z]', '', ctt)
                post.postContent = ctt

            
    
    def getPageUrl(self, thememode):
        if thememode:
            return self.postListUrlTheme % (self.boardname,)
        else:
            return self.postListUrl % (self.boardname,)
    
    
    def setdata(self, content, postlist):
        ls = re.findall(r'(docWriter\(.*\)\;)', content, re.M)
        if len(ls):
            str = ls[0]
            pattern = r"docWriter\('.*?',(?P<bid>[0-9]+),.*\);"
            m = re.match(pattern , str)
            if m and m.group('bid'):
                for p in postlist:
                    p.postLink = self.postContentUrl %(m.group('bid'), p.getID())
                    p.setData('bid', m.group('bid'))
                        
    def parsePost(self,data):
        line =  unicode(data)
        pattern = r"c\.o\((?P<id>\d+),(?P<idd>\d+),'(?P<userid>[0-9a-zA-Z]+)','.*',(?P<time>\d+),'(?P<title>.*)',.*\)\;"
        m = re.match(pattern, line)
        postID = m.group('id')
        postUser = m.group('userid')
        postTime = parsetime(string.atoi(m.group('time')))
        postTitle = m.group('title')
        postLink = self.postUrl %(self.boardname, postID)
        p= model.Post(title= postTitle, time=postTime, id = postID, user=postUser,link = postLink)
        return p
    
    def getCopy(self):
        newAdapter = ConcreteAdapter()
        newAdapter.boardname = self.boardname
        return newAdapter
    

import time
def parsetime(t):
    return time.localtime(t)